package com.zone.module_base.fragment

import android.Manifest
import android.content.Intent
import android.util.Log
import com.zone.core.base.BaseBindingViewModelFragment
import com.zone.module_base.BaseApp
import com.zone.module_base.databinding.FragmentTestFileBinding
import com.zone.module_base.viewmodel.TestFunctionViewModel


class TestFileFragment :
    BaseBindingViewModelFragment<FragmentTestFileBinding, TestFunctionViewModel>() {
    lateinit var bind: FragmentTestFileBinding

    override fun inject() {
        BaseApp.appComponent.inject(this)
    }

    override fun onEvent(eventId: Int) {
        super.onEvent(eventId)

        when (eventId) {
            1 -> {//保存文件

            }

            2 -> {//读取

            }

            3 -> {//读取Assets

            }

            4 -> {//文件读写权限
                val filePermissions = arrayOf(
                    Manifest.permission.READ_EXTERNAL_STORAGE,//读取外部存储
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,//写入外部存储
                )
                requestPermissions(filePermissions, onGranted = {
                    viewModel.saveToFile()
                })
            }
        }
    }

    override fun initDataBinding(binding: FragmentTestFileBinding) {
        super.initDataBinding(binding)
        bind = binding

        bind.rgGroup.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                bind.radioButton1.id -> {
                    viewModel.selectFileType.value = 0
                }

                bind.radioButton2.id -> {
                    viewModel.selectFileType.value = 1
                }

                bind.radioButton3.id -> {
                    viewModel.selectFileType.value = 2
                }
            }
        }


    }


    override fun onActivityResult(
        requestCode: Int, resultCode: Int, resultData: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, resultData)
        Log.d(
            "TAG",
            "onActivityResult: =========$requestCode=========================$resultCode=====================$resultData"
        )
        resultData?.data?.path

    }
}